package servlets;

import com.fasterxml.jackson.databind.ObjectMapper;
import dao.impl.PatientDaoImpl;
import entity.Patient;
import util.GetInitPwdById;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author:
 * @Date: 2020-05-01
 * @Description: @PACKAGE:${PACKAGE_NAME}
 * @version: 1.0
 */
@WebServlet(name = "病人信息操作", value = "/operatePatientInfoServlet")
public class OperatePatientInfoServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String json = "";
        int i = 0;
        PatientDaoImpl patientDao = new PatientDaoImpl();
        String flag = request.getParameter("purpose");
        String id = request.getParameter("Patient_id");
        String pwd = GetInitPwdById.Get(id);
        String name = request.getParameter("Patient_name");
        String age = request.getParameter("Patient_age");
        String birthday = request.getParameter("Patient_birthday");
        String creattime = request.getParameter("Patient_creattime");
        String sex = request.getParameter("Patient_sex");
        String tel = request.getParameter("Patient_tel");
        String picture = request.getParameter("patient_picture");
        if(sex.equals("男")) {
        	picture="image/病人素材头像男.jpg";
        }else {
        	picture="image/病人素材头像女.jpg";
        }
        String familyName = request.getParameter("Patient_family_name");
        String familyTel = request.getParameter("Patient_family_tel");
        Patient patient = new Patient(id, name, sex, age, familyName, tel, picture, pwd);
        if (flag.equals("0")) {
            i = patientDao.add(patient);
        } else if (flag.equals("1")) {
            i = patientDao.updateNoPwdNoPicture(patient);

        }
        if (i == 1) {
            json = "{\"flag\":\"提交成功\"}";
        } else {
            json = "{\"flag\":\"提交失败，请重试\"}";
        }

        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(response.getWriter(), json);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}